<template>
  <div class="subjects-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>
          <!-- 面包屑导航 -->
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>大数据</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 面包屑导航 -->
        </span>
      </div>
      <!-- from -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="学科名称">
          <el-input
            placeholder="请输入内容"
            v-model="requestParameters.subjectName"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="" @click="inpt">清除</el-button>
          <el-button type="primary" @click="handleSarech">搜索</el-button>
        </el-form-item>
        <!-- 新增按钮 -->
        <el-form-item class="button fr">
          <el-row>
            <el-button
              class="el-button fr"
              type="success"
              icon="el-icon-edit"
              @click="addDialogVisible = true"
              >新增学科</el-button
            >
          </el-row>
        </el-form-item>
        <!-- 新增按钮 -->
      </el-form>
      <!-- from -->
      <!-- 数据总条数 -->
      <el-alert type="info" show-icon :title="alertText"> </el-alert>
      <!-- 数据总条数 -->
      <!-- 数据表格 -->
      <!-- 数据列表 -->
      <el-table
        :data="dataList"
        ref="requestParameters"
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%"
        border
      >
        <el-table-column
          class="xuhao"
          align="center"
          label="序号"
          type="index"
          :index="getIndex"
        >
          <!-- :index="getIndex" 定义自动增长 -->
        </el-table-column>
        <el-table-column align="center" label="学科名称">
          <template slot-scope="scope">
            <span>{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建者">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{i}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="前台是否显示">
          <template slot-scope="scope">
            <!-- scope.row.isFrontDisplay===1?'是':'否' 判断显示--是--否 -->
            <span >{{scope.row.isFrontDisplay===1?'是':'否'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="二级目录">
          <template slot-scope="scope">
            <span>{{ scope.row.twoLevelDirectory }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="标签">
          <template slot-scope="scope">
            <span>{{ scope.row.tags }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="题目数量">
          <template slot-scope="scope">
            <span>{{ scope.row.totals }}</span>
          </template>
        </el-table-column>
        <!-- 操作按钮 -->
        <el-table-column
          align="center"
          label="操作"
          width="280"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" class="caozuo-cell">
            <el-link type="primary" size="mini">
              <router-link
                :to="`/subjects/directorys?id=${scope.row.id}&name=${scope.row.subjectName}`"
                >学科分类</router-link
              >
            </el-link>
            <el-link type="primary" size="mini">
              <router-link
                :to="`/subjects/tags?id=${scope.row.id}&name=${scope.row.subjectName}`"
                >学科标签</router-link
              >
            </el-link>
            <el-link type="primary" size="mini" @click="ShowAlter(scope.row)"
              >修改</el-link
            >
            <el-link
              type="primary"
              size="mini"
              @click="ShowRemoveBuke(scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- end -->
      <!-- 数据表格 -->
      <!-- 分页          @size-change="handleSizeChange"
              @current-change="handleCurrentChange"-->
      <div class="pagination">
        <div class="pages">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="Number(requestParameters.page)"
            :total="Number(total)"
            :page-size="Number(requestParameters.pagesize)"
            :page-sizes="[2, 5, 7, 10]"
            layout="sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
      <!-- end -->
    </el-card>
    <!-- 新增弹框 -->
    <el-dialog title="新增学科" :visible.sync="addDialogVisible" width="30%">
      <!-- 内容主体 -->
      <el-form
        :model="addFrom"
        :rules="addruleForm"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="addFrom.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch v-model="addFrom.isFrontDisplay"></el-switch>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addbuke">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增弹框 -->

    <!-- 修改弹框 -->
    <el-dialog title="修改学科" :visible.sync="editDialogVisible" width="30%">
      <!-- 内容主体 -->
      <el-form
        :model="editFrom"
        :rules="editRuleForm"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="editFrom.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch v-model="editFrom.isFrontDisplay"></el-switch>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatebuke">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹框 -->
    <!-- 删除 -->

    <!-- 删除 -->
  </div>
</template>

<script>
import { list, add, update, detail, remove } from '@/api/hmmm/subjects.js'
export default {
  name: 'Subjects',
  components: {},
  data () {
    return {
      dataList: [],
      total: null, // 总条数
      alertText: '', // 记录总条
      requestParameters: {
        page: 1,
        pagesize: 5,
        subjectName: ''
      },
      // 控制添加学科
      addDialogVisible: false,
      // 添加学科的表单数据
      addFrom: {
        subjectName: '',
        isFrontDisplay: false
      },
      // 学科的验证规则对象
      addruleForm: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '学科名称的长度在3~10个字符之间',
            trigger: 'blur'
          }
        ]
      },
      // 修改
      editFrom: {
        subjectName: '',
        isFrontDisplay: false
      },
      // 控制修改学科对话框的显示与隐藏
      editDialogVisible: false,
      // 修改学科的验证规则对象
      editRuleForm: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '学科名称的长度在3~10个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList (params) {
      const { data } = await list(this.requestParameters)
      // 获取数据给dataList
      this.dataList = data.items
      // 获取总条数
      this.total = data.counts
      this.alertText = `共 ${this.total} 条记录`
      console.log(data)
    },
    // 每页显示条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      this.getList(this.requestParameters)
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getList()
    },
    // 序号自动增长 getIndex
    getIndex (index) {
      return (
        (this.requestParameters.page - 1) * this.requestParameters.pagesize + index + 1)
    },
    // 清除、
    inpt () {
      this.input = ''
      this.requestParameters.subjectName = ''
      this.getList()
    },
    // 搜索
    handleSarech () {
      this.requestParameters.page = 1
      this.getList(this.requestParameters)
    },
    // 添加学科
    async addbuke () {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加学科的网络请求
        const { data: res } = await add(this.addFrom)
        if (res.status !== 201) {
          this.$message.error('添加学科失败！')
        }
        this.addFrom.subjectName = ''
        this.$message.success('添加学科成功！')
        // 隐藏添加学科的对话框
        this.addDialogVisible = false
        // 重新获取学科列表数据
        this.getList()
      })
    },

    // 修改
    async ShowAlter (id) {
      const { data: res } = await detail(id)
      // console.log(data)
      this.editDialogVisible = true
      console.log(res)
      this.editFrom = res
      // 判断Switch 开关显示
      if (this.editFrom.isFrontDisplay === 0) {
        this.editFrom.isFrontDisplay = false
      } else if (this.editFrom.isFrontDisplay === 1) {
        this.editFrom.isFrontDisplay = true
      } else {
        return
      }
      console.log(this.editFrom)
    },
    // 修改添加数据信息并提交
    updatebuke () {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加学科的网络请求
        const { data: res } = await update(this.editFrom)

        if (res.status !== 201) {
          this.$message.error('修改学科失败！')
        }
        this.$message.success('修改学科成功！')
        // 隐藏添加学科的对话框
        this.editDialogVisible = false
        // 重新获取学科列表数据
        this.getList()
      })
    },
    // 删除
    async ShowRemoveBuke (id) {
      // 弹框询问学科是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该学科, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((error) => error)

      // 如果学科确认删除，则返回值为字符串 confirm
      // 如果学科取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await remove(id)
      console.log(res)
      if (res.success !== true) {
        return this.$message.error('删除学科失败！')
      }
      this.$message.success('删除学科成功！')
      this.getList()
    },
    // 路由跳转
    touterTo () {
      this.$router.push({
        path: '/subjects/dashboard',
        query: { id: this.id, name: this.name }
      })
    }
  }
}
</script>

<style scoped lang='less'>
.subjects-container {
  padding: 20px;
  .el-table {
    /deep/.el-table_1_column_1 {
      .cell {
        padding: 0 !important;
        white-space: nowrap;
        width: 50px;
        text-align: center;
      }
    }
   /deep/.is-leaf{
       text-align: center !important;
        white-space: nowrap;
    }
    // .el-table_1_column_4  {
    //   .cell{

    //   }
    // }
  }
}
.fr {
  float: right !important;
}
.el-table {
  margin-top: 10px;
}

/deep/.el-link {
  margin: 5px;
}
/deep/.el-pagination {
  margin: 10px;
  /* margin-right: 0;   .is-background*/
  float: right;
}
/deep/.el-table th > .cell {
  padding: 0;
}
</style>
